package com.link.base.base.purchaseplan.service;

import com.link.base.user.model.CoreUser;
import com.link.base.base.purchaseplan.dao.mybatis.mapper.PurchasePlanItemMapper;
import com.link.base.base.purchaseplan.model.PurchasePlan;
import com.link.base.base.purchaseplan.model.PurchasePlanItem;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.query.Filter;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.QueryParamUtil;
import com.link.core.util.StringValidateUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class PurchasePlanItemServiceImpl extends BasicServiceImpl<PurchasePlanItem> implements PurchasePlanItemService {

    @Resource
    private PurchasePlanItemMapper purchasePlanItemMapper;

    @Resource
    private PurchasePlanService purchasePlanService;

    @Override
    public BasicMapper<PurchasePlanItem> getBasicMapper() {
        return purchasePlanItemMapper;
    }

    @Override
    public void beforInsert(PurchasePlanItem entity) throws Exception {
        CoreUser user = UserUtil.getUser();
        entity.setPostnId(user.getPostnId());
        entity.setOrgId(user.getOrgId());
        // 新增检查产品重复问题
        PurchasePlanItem query = (PurchasePlanItem) QueryParamUtil.addQpsFilter(PurchasePlanItem.class,
                new Filter("prodId", String.valueOf(entity.getProdId())),
                new Filter("headId", String.valueOf(entity.getHeadId()))
        );
        if (!CollectionUtils.isEmpty(this.queryByExamplePage(query))) {
            throw new ServiceException("PURCHASE-001");
        }
    }

    @Override
    public void beforUpdate(PurchasePlanItem entity) throws Exception {
        if (!StringValidateUtils.isInteger(entity.getQty())) {
            throw new ServiceException("PRODUCT-029");
        }
    }

    @Transactional(rollbackFor = {ServiceException.class})
    @Override
    public void batchUpsert(List<PurchasePlanItem> list) throws Exception {
        CoreUser user = UserUtil.getUser();
        Long postnId = user.getPostnId();
        for (int i = 0; list != null && i < list.size(); ++i) {
            // 校验并新增要货计划单头记录
            PurchasePlanItem item = list.get(i);
            item.setPostnId(postnId);
            boolean isInsertFlag = isInsertFlag(item);
            if (isInsertFlag) {
                PurchasePlan record = new PurchasePlan();
                String accountNum = item.getAccountNum();
                String periodName = item.getPeriodName();
                record.setAccountNum(accountNum);
                record.setPeriodName(periodName);
                PurchasePlan purchasePlan = purchasePlanService.queryRepeatOne(record);
                if (purchasePlan == null || purchasePlan.getId() == null) {
                    throw new ServiceException("PURCHASE-002！");
                }
                if (!purchasePlan.getPostnId().equals(postnId)) {
                    throw new ServiceException("PURCHASE-003");
                }
                item.setHeadId(purchasePlan.getId());
            }
            this.upsert(item);
        }
    }




}

